package com.wms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.dto.ConfirmOutOrderDto;
import com.wms.dto.OutOrderDto;
import com.wms.pojo.OutboundOrderDetails;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.service.OutboundOrderDetailsService;
import com.wms.service.OutboundOrderService;
import com.wms.vo.OutboundOutVo;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/outboundOrder")
@RequiredArgsConstructor
public class OutboundOrderController {
    private final OutboundOrderService outboundOrderService;
    private final OutboundOrderDetailsService detailsService;

    @GetMapping
    public Result queryPage(Integer page,Integer pageSize,Integer status,String invoiceCode,String customerName){
        PageResult pageResult = outboundOrderService.queryPage(page,pageSize,status,invoiceCode,customerName);
        return Result.success(pageResult);
    }

    //添加数据
    @PostMapping
    public Result saveOutBoundOrder(@RequestBody OutOrderDto dto){
        outboundOrderService.saveOutBoundOrder(dto);
        return Result.success();
    }

    //回显数据
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){
        OutOrderDto dto = outboundOrderService.queryById(id);
        return Result.success(dto);
    }

    //删除数据
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(@PathVariable Integer id){
        outboundOrderService.removeById(id);
        detailsService.remove(Wrappers.<OutboundOrderDetails>lambdaQuery()
                .eq(OutboundOrderDetails::getOutboundOrderId,id));
        return Result.success();
    }

    //确定发货单
    @PutMapping
    public Result updateById(@RequestBody ConfirmOutOrderDto dto){
        outboundOrderService.updateByIdStatus(dto);
        return Result.success();
    }


    //出库的条件查询
    @GetMapping("/out")
    public Result queryPageOut(Integer page,Integer pageSize, String invoiceCode,String customerName,String skuTitle){
        PageResult pageResult = outboundOrderService.queryPageOut(page,pageSize,invoiceCode,customerName,skuTitle);
        return Result.success(pageResult);
    }

    //出库的确认发货的状态修改
    @PutMapping("/out")
    public Result updateByIdStatus(@RequestBody OutboundOutVo outbound){
        outboundOrderService.updateByIdOutbound(outbound);
        return Result.success();
    }

    //签收的条件查询
    @GetMapping("/receive")
    public Result queryPageReceive(Integer page,Integer pageSize, String invoiceCode,String customerName,String skuTitle){
        PageResult pageResult = outboundOrderService.queryPageReceive(page,pageSize,invoiceCode,customerName,skuTitle);
        return Result.success(pageResult);
    }

    //确定签收的状态修改
    @PutMapping("/receive")
    public Result updateByIdReceiveStatus(@RequestBody OutboundOutVo outbound){
        outboundOrderService.updateByIdReceiveStatus(outbound);
        return Result.success();
    }
}
    